import { ajaxGet, jsonPost, ajaxPost } from '@/util'

export default ({
  state: {
    logisticsList: [], // 物流
    logisticsLoading: false,
    brandList: [], // 品牌
    brandLoading: false,
    categoryList: [], // 分类
    categoryLoading: false,
    shopList: [], // 店铺分类
    shopLoading: false,
    rankList: [], // 等级
    rankLoading: false,
    // storeTypeData: [{ value: 'CONTRACT', text: '合约店' }, { value: 'PRESENCE', text: '形象店' }, { value: 'AUTOSPANCE', text: '车空间' }, { value: 'UNCONTRACT', text: '体验店' }, { value: 'FLAGSHIP', text: '旗舰店' }, { value: 'CONTRACTPLUS', text: '合约店+' }, { value: 'CPS', text: 'cps' }, { value: 'PRESENCEPLUS', text: '形象店+' }],
    storeTypeData: [],
    storeTypeList: [], //门店类型列表，
    storeList: [], //门店列表
    quarterCheckStatus: [{value: 0, text: '未审核'}, {value:1, text: '已审核'}], // 月、季度积分返利审核状态
    fundCheckStatus: [{value: 4, text: '全部'}, {value: 0, text: '待评分'}, {value: 1, text: '待审核'}, {value: 2, text: '审核通过'}, {value: 3, text: '审核不通过'}], // 门店基金审核状态
    integralAuditStatus:[{value: 0, text: '待审核'}, {value: 1, text: '审核通过'}, {value: 2, text: '审核不通过'}], // 门店积分审核审核状态
    wareHouseUseTypeList: [{ value: 1, text: '启用' }, { value: 0, text: '不启用' }], //仓库启用状态
    enableTypeList: [{ value: true, text: '是' }, { value: false, text: '否' }], //启用状态
    starTypeList: [{ value: true, text: '有星标' }, { value: false, text: '无星标' }, ],
    dealerList: [],
    quarterList: [{value: 1, text: 1},{value: 2, text: 2},{value: 3, text: 3},{value: 4, text: 4}], //季度列表
    contractTypeList: [{ value: '0', text: '待生效' }, { value: '1', text: '生效' }, { value: '2', text: '失效' }],
    isHave: [{ value: true, text: '有仓库' }, { value: false, text: '无仓库' }], // 仓库地址
  },
  mutations: {
    setData(state, param) {
      let [key, data, id, name] = param
      data = data || []
      state[key] = data.map(item => {
        return {
          ...item,
          value: item[id || 'id'],
          text: item[name || 'name']
        }
      })
    },
    setCategoryData(state, param) {
      state['categoryList'] = param
    },
    setShopData(state, param) {
      state['shopList'] = param
    },
    setLoading(state, param) {
      let [key, loading] = param
      state[key] = loading
    }
  },
  actions: {
    // 请求物流数据
    getLogisticsList({ commit, dispatch, state }, needRefresh) {
      if (needRefresh || (!state.logisticsList.length && !state.logisticsLoading)) {
        commit('setLoading', ['logisticsLoading', true])
        return ajaxGet('/common/logistics/list').then(data => {
          commit('setData', ['logisticsList', data, 'id', 'name'])
          commit('setLoading', ['logisticsLoading', false])
        })
      } else {
        return state.logisticsList
      }
    },
    // 请求品牌数据
    getBrandList({ commit, dispatch, state }, needRefresh) {
      if (needRefresh || (!state.brandList.length && !state.brandLoading)) {
        commit('setLoading', ['brandLoading', true])
        return ajaxGet('/product/common/supplier_brand').then(data => {
          commit('setData', ['brandList', data, 'brandId', 'brandName'])
          commit('setLoading', ['brandLoading', false])
        })
      } else {
        return state.brandList
      }
    },
    // 请求分类数据
    getCategoryList({ commit, dispatch, state }, needRefresh) {
      if (needRefresh || (!state.categoryList.length && !state.categoryLoading)) {
        commit('setLoading', ['categoryLoading', true])
        return ajaxGet('/product/common/supplier_tree', { isEnabled: true }).then(data => {
          let list = (data || []).map(parent => {
            let parentTarget = {
              id: parent.categoryDTO.id,
              label: parent.categoryDTO.name,
            }
            if (parent.childCategoryList.length) {
              let childList = parent.childCategoryList.map(child => {
                let childTarget = {
                  id: child.categoryDTO.id,
                  label: child.categoryDTO.name
                }
                if (child.childCategoryList.length) {
                  let grandsonList = child.childCategoryList.map(grandson => {
                    return {
                      id: grandson.categoryDTO.id,
                      label: grandson.categoryDTO.name
                    }
                  })
                  childTarget.children = grandsonList
                }
                return childTarget
              })
              parentTarget.children = childList
            }
            return parentTarget
          })
          commit('setCategoryData', list)
          commit('setLoading', ['categoryLoading', false])
        })
      } else {
        return state.categoryList
      }
    },
    // 请求店铺数据
    getShopList({ commit, dispatch, state }, needRefresh) {
      if (needRefresh || (!state.shopList.length && !state.shopLoading)) {
        commit('setLoading', ['shopLoading', true])
        return ajaxGet('/product/supplier_category/tree').then(data => {
          let list = (data || []).map(parent => {
            let parentTarget = {
              id: parent.categoryDTO.id,
              label: parent.categoryDTO.name
            }
            if (parent.childCategoryList.length) {
              let childList = parent.childCategoryList.map(child => {
                let childTarget = {
                  id: child.categoryDTO.id,
                  label: child.categoryDTO.name
                }
                if (child.childCategoryList.length) {
                  let grandsonList = child.childCategoryList.map(grandson => {
                    let grandsonTarget = {
                      id: grandson.categoryDTO.id,
                      label: grandson.categoryDTO.name
                    }
                    return grandsonTarget
                  })
                  childTarget.children = grandsonList
                }
                return childTarget
              })
              parentTarget.children = childList
            }
            return parentTarget
          })
          commit('setShopData', list)
          commit('setLoading', ['shopLoading', false])
        })
      } else {
        return state.shopList
      }
    },
    // 请求等级数据
    getRankList({ commit, dispatch, state }, needRefresh) {
      if (needRefresh || (!state.rankList.length && !state.rankLoading)) {
        commit('setLoading', ['rankLoading', true])
        return ajaxGet('/common/user/rank/list').then(data => {
          commit('setData', ['rankList', data, 'id', 'rankName'])
          commit('setLoading', ['rankLoading', false])
        })
      } else {
        return state.rankList
      }
    },
    // 根据参数或经销商
    getDealerList({ commit, dispatch, state }, params) {
      return ajaxGet('/distributor/contract/pageSignedDistributor', params).then(data => {
        state.dealerList = (data.records || []).map(item => {
          item.value = item.distributorId
          item.text = item.distributorName
          return item
        })
      })
    },
    getDealerListByDpt({ commit, dispatch, state }, params) {
      return jsonPost(`/department/queryDistributorDepartment`, params).then(data => {
        state.dealerList = (data.records || []).map(item => {
          item.value = item.distributorId // distributor_id有可能一样
          item.text = item.distributorName
          return item
        })
      })
    },
     // 获取所有门店类型列表
     getStoreTypeList({ commit, dispatch, state }) {
       if (!state.storeTypeList.length) {
        return ajaxGet('/storeType/findAll').then(data => {
          state.storeTypeList = data.map(item => {
            return {
              value: item.id,
              text: item.name
            }
          })
        })
      } else {
        return state.storeTypeList
      }
    },
    // 获取所有门店类型列表 新
    getStoreType({ commit, dispatch, state }) {
      if (!state.storeTypeData.length) {
        return new Promise((resolve, reject) => {
          return ajaxPost('/common/store_type/all').then(data => {
            state.storeTypeData = (data || []).map(item => {
              return {
                value: item.id,
                text: item.name
              }
            })
            resolve(state.storeTypeData)
          })
        })
     } else {
       return state.storeTypeData
     }
   },
    // 获取所有门店
    getStoreList({commit, dispatch, state}, params) {
      return new Promise((resolve, reject) => {
        ajaxGet('/tiger/store/page_for_activity', params).then(res => {
          resolve(res)
          const records = res.records || []
          state.storeList = records.map(item => {
            return {
              value: item.id,
              label: item.storeName
            }
          })
        })
      })
    },
    // 获取所有经销商
    getDistributorList({commit, dispatch, state}, params) {
      return new Promise((resolve, reject) => {
        ajaxGet('/tiger/distributor/page_for_activity', params).then(res => {
          resolve(res)
          const records = res.records || []
          state.storeList = records.map(item  => {
            return {
              value: item.id
            }
          })
        })
      })
    }
  },
  getters: {
    logisticsList: (state) => state.logisticsList,
    brandList: (state) => state.brandList,
    categoryList: (state) => state.categoryList,
    shopList: (state) => state.shopList,
    rankList: (state) => state.rankList,
    storeTypeList: (state) => state.storeTypeList,
    quarterCheckStatus: (state) => state.quarterCheckStatus,
    fundCheckStatus: (state) => state.fundCheckStatus,
    wareHouseUseTypeList: (state) => state.wareHouseUseTypeList,
    starTypeList: (state) => state.starTypeList,
    enableTypeList: (state) => state.enableTypeList,
    storeTypeData: (state) => state.storeTypeData,
    dealerList: (state) => state.dealerList,
    quarterList: (state) => state.quarterList,
    contractTypeList: (state) => state.contractTypeList,
    isHave: (state) => state.isHave,
    integralAuditStatus: (state) => state.integralAuditStatus
  }
})
